// Copyright (c) 2009-2010, Jeremy Brewer
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

package galaxie500.benchmarks;

import java.util.ArrayList;
import java.util.Random;

import galaxie500.datastructures.BoundingBox;
import galaxie500.datastructures.DataSet;
import galaxie500.datastructures.KDTree;
import galaxie500.datastructures.MatrixDataSet;
import galaxie500.datastructures.Point;
import galaxie500.util.Timer;

/**
 * Simple benchmark for KDTRee code
 * 
 * @author Jeremy Brewer
 */
public class KDTreeBenchmark {

  /**
   * Generates random data in 4 dimensions and searches it using a KDTree.
   * 
   * @param args Not used
   */
  public static void main(String[] args) {
    int dimension = 4;
    int numPoints = 500000;
    double[] errors = new double[dimension];
    double[][] data = new double[numPoints][dimension];
    Random random = new Random(114);

    // This error gives ~10 or fewer points on average within search.
    for (int i = 0; i < dimension; i++) {
      errors[i] = 0.025;
    }

    for (int i = 0; i < numPoints; i++) {
      for (int j = 0; j < dimension; j++) {
        data[i][j] = random.nextDouble();
      }
    }

    DataSet dataset = new MatrixDataSet(data);
    KDTree kdtree = new KDTree(dataset);

    Timer timer = new Timer();
    timer.start();
    timer.stop();
    System.out.printf("Tree build time was %.3f sec\n", timer.getTimeSec());
    timer.reset();

    ArrayList<KDTree.Match> closestPoints = new ArrayList<KDTree.Match>();

    double[] minValues = new double[dimension];
    double[] maxValues = new double[dimension];
    BoundingBox boundingBox = new BoundingBox();
    timer.start();

    for (int i = 0; i < numPoints; i++) {
      Point point = dataset.getPoint(i);

      // Update bounding box.
      for (int j = 0; j < dimension; j++) {
        minValues[j] = point.getValue(j) - errors[j];
        maxValues[j] = point.getValue(j) + errors[j];
      }
      boundingBox.reset(minValues, maxValues);

      // Fast search.
      // NOTE: this will return the point itself too.
      kdtree.search(point, boundingBox, closestPoints);
    }

    timer.stop();
    double searchTime = timer.getTimeSec();
    System.out.printf("Tree search time was %.3f sec\n", searchTime);
    System.out.printf("In 4D made %e queries per sec\n", (double) numPoints /
        searchTime);
  }
}
